/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Main.java
 *
 * Created on Feb 14, 2011, 9:56:59 AM
 */

package lanchatprojectclient;

/**
 *
 * @author Achilles8x
 */
import java.awt.image.ImageProducer;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.event.*;
import java.math.BigInteger;

import protocols.*;
import sun.security.rsa.RSASignature.SHA512withRSA;




public final class HackerInterfaceDesign extends JFrame {
    private ChatHacker client;
    protected Vector messageWindows;
    protected Vector tabToClient;

    /** Creates new form Main */
    HackerInterfaceDesign(ChatHacker client) {
        initComponents();        
        setUpTabs();
        this.client = client;

        connectMenuItem.setEnabled( true );
        logoutMenuItem.setEnabled(false);
	messagetypeTextField.setEnabled( false );
        tbChatWindows.setEnabled(false);
        statusTextField.setEnabled( false );
        sendButton.setEnabled( false );
        okButton.setEnabled( false );
	lstClients.setEnabled( false );
        changepasswordMenuItem.setEnabled( false );
        exitMenuItem.setEnabled(true);
        ((JTextArea)messageWindows.elementAt(0)).setText("");

        messagetypeTextField.requestFocus();
        statusTextField.requestFocus();
    }


   protected void setUpTabs()
	{
		messageWindows = new Vector(5,2);
		tabToClient = new Vector(5,2);
		//Tạo tab chính (chat room), tab  này không thể đóng
		messageWindows.addElement(new JTextArea(10,30));
		((JTextArea)messageWindows.elementAt(0)).setLineWrap(true);
		((JTextArea)messageWindows.elementAt(0)).setEditable(false);

		tbChatWindows.addTab( "Public",new JScrollPane( (JTextArea)
							messageWindows.elementAt(0),
							JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
		tbChatWindows.setIconAt( 0,new ImageIcon( "icons/My-User.gif" ) );
	}



    //Mởi 1 tab mới để chat riêng tư
	void openNewTab( int friendId )
	{
		//Kiểm tra 1 tab với đã mới với người dùng chưa
		//Nếu có rồi thì tập trung vào tab này
		int index = tabToClient.indexOf( new Integer( friendId ) );
		if( index != -1 )
		{
			tbChatWindows.setSelectedIndex( index );
			return;
		}
		messageWindows.addElement( new JTextArea(10,30) );
		tabToClient.addElement( new Integer(friendId) );
		((JTextArea)messageWindows.lastElement()).setLineWrap(true);
		((JTextArea)messageWindows.lastElement()).setEditable(false);
		lstClients.setSelectedIndex( friendId );
		tbChatWindows.addTab( (String)lstClients.getSelectedValue(),(new JScrollPane( (JTextArea)messageWindows.lastElement() ) ) );
		tbChatWindows.setIconAt( tabToClient.size(),new ImageIcon( "icons/L-Look_128.gif" ) );

		tabMenu.setEnabled( true );
		//closeTabMenu.setEnabled( true );
		//closeAllTabsMenu.setEnabled( true );
	}



	private void showMessage( int tabSelected, String message )
	{
		((JTextArea)messageWindows.elementAt(tabSelected)).append( message );
	}

	void showMessage( Message userMsg )
	{
		int tabIndex = 0;

		//Nếu chat room thì hiển thị ở tab chính
		//Nếu chat riêng thì hiển thị ở tab tương ứng
		if( ((Message)userMsg).audience == true )
		{
			((JTextArea)messageWindows.elementAt(tabIndex)).append( ((Message)userMsg).message + "\n");
		}
		else
		{
			tabIndex = tabToClient.indexOf( new Integer(((Message)userMsg).senderId) );

			((JTextArea)messageWindows.elementAt(tabIndex+1)).append( ((Message)userMsg).message + "\n");
		}
	}

        void showMessageRSA( MessageRSA userMsg )
	{
		int tabIndex = 0;

		//Nếu chat room thì hiển thị ở tab chính
		//Nếu chat riêng thì hiển thị ở tab tương ứng
		if( ((MessageRSA)userMsg).audience == true )
		{
			((JTextArea)messageWindows.elementAt(tabIndex)).append( ((MessageRSA)userMsg).message + "\n");
		}
		else
		{
			tabIndex = tabToClient.indexOf( new Integer(((MessageRSA)userMsg).senderId) );

			((JTextArea)messageWindows.elementAt(tabIndex+1)).append( ((MessageRSA)userMsg).RSAmessage + "\n");
		}
	}
       


	protected void sendMessage()
	{
		String str = messagetypeTextField.getText();
		int tabSelected = tbChatWindows.getSelectedIndex();

		if( str.length() != 0 )
		{
			try
			{
				if( tabSelected == 0 )
				{
					client.sendPublicMessage( str );
				}
				else
				{
					Integer clientIndex = (Integer)tabToClient.elementAt( tabSelected-1 );
					client.sendPrivateMessage( clientIndex.intValue(),str );
					((JTextArea)messageWindows.elementAt( tabSelected )).append(client.Name + " nói > " + str + "\n");
				}
			}
			catch( IOException io_ex )
			{
				showMessage( tabSelected,"\n\nKhông thể gửi tin...\n\n" );
			}
			messagetypeTextField.setText("");
		}
	}

       

       

        protected void sendOfflineMessage(String str)
	{
		int tabSelected = tbChatWindows.getSelectedIndex();

		if( str.length() != 0 )
		{
				if( tabSelected == 0 )
				{
					((JTextArea)messageWindows.elementAt( tabSelected )).append("Tin nhắn offline:  " + str + "\n");
				}
		}
	}

        protected void sendStatus() throws IOException
	{
		client.Status = statusTextField.getText();
                client.sendStt(client.Status);
		statusTextField.setText("");
	}

	//Đóng tất cả tab ngoại trừ tab chính
	protected void closeAllTabs()
	{
		int index = 1;
		while( index != tbChatWindows.getTabCount() )
		{
			tbChatWindows.removeTabAt( index );
			messageWindows.removeElementAt( index );
		}
		tabToClient.clear();

		tabMenu.setEnabled( false );
//		closeTabMenu.setEnabled( false );
//		closeAllTabsMenu.setEnabled( false );
	}



	//Xử lý khi 1 người dùng thoát
	void notifyUserLeft( String clientName )
	{
		int friendId = client.clientList.indexOf( clientName );
		int index = tabToClient.indexOf( new Integer( friendId ) );

		//Nếu người dùng chat với ai đó
		if( index == -1 )
			return;
                String[] temp = clientName.split(">>");
                String NameLeft = temp[1];

		JOptionPane.showMessageDialog( client.window,NameLeft+" đã thoát","Người dùng thoát",JOptionPane.INFORMATION_MESSAGE );

		tbChatWindows.removeTabAt( index+1 );
		messageWindows.removeElementAt( index+1 );
		tabToClient.removeElementAt( index );

		//Nếu chỉ có tab chính
		if( tbChatWindows.getTabCount() == 1 )
		{
			tabMenu.setEnabled( false );
			//closeTabMenu.setEnabled( false );
			//closeAllTabsMenu.setEnabled( false );
		}
	}

	/*class OnExit extends WindowAdapter implements WindowListener
	{
		public void windowClosing( WindowEvent we )
		{
                try {
                    JOptionPane.showMessageDialog( client.window,"Bạn chỉ có thể thoát khi đã kết nối"+"\nvì chương trình client phải gửi thông báo cho server","Người dùng thoát",JOptionPane.INFORMATION_MESSAGE );
                        client.sendLogout(true);
                        System.exit(0);
                } catch (IOException ex) {
                    Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
                }
		}

		public void windowActivated(WindowEvent e)
		{
	        messagetypeTextField.requestFocus();
                statusTextField.requestFocus();
    	}
	}*/



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        accountTextField = new javax.swing.JTextField();
        passwordTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        loginButton = new javax.swing.JButton();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jDialog3 = new javax.swing.JDialog();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        accountTextField1 = new javax.swing.JTextField();
        passwordTextField1 = new javax.swing.JTextField();
        loginButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jMenuBar3 = new javax.swing.JMenuBar();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        messagetypeTextField = new javax.swing.JTextField();
        clientlistScrollPane = new javax.swing.JScrollPane();
        lstClients = new javax.swing.JList();
        statusTextField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        tbChatWindows = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        connectMenuItem = new javax.swing.JMenuItem();
        configMenuItem = new javax.swing.JMenuItem();
        changepasswordMenuItem = new javax.swing.JMenuItem();
        logoutMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        tabMenu = new javax.swing.JMenu();
        helpMenu = new javax.swing.JMenu();

        jLabel2.setText("Tài khoản");

        jLabel3.setText("Mật khẩu");

        accountTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accountTextFieldActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Nhớ trạng thái đăng nhập");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jCheckBox2.setText("Tự động đăng nhập");

        jCheckBox3.setText("Đăng nhập ẩn");

        loginButton.setText("Đăng nhập");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        jMenu4.setText("Điều khiển");

        jMenuItem5.setText("Thoát");
        jMenu4.add(jMenuItem5);

        jMenuBar2.add(jMenu4);

        jMenu5.setText("Trợ giúp");
        jMenuBar2.add(jMenu5);

        jDialog1.setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(jLabel4)
                .addContainerGap(176, Short.MAX_VALUE))
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jCheckBox3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(passwordTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                        .addComponent(accountTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jDialog1Layout.createSequentialGroup()
                        .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(loginButton)
                            .addComponent(jCheckBox2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(36, 36, 36))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(accountTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox3)
                .addGap(31, 31, 31)
                .addComponent(loginButton)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jLabel5.setText("Mật khẩu cũ");

        jLabel6.setText("Mật khẩu mới");

        accountTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accountTextField1ActionPerformed(evt);
            }
        });

        loginButton1.setText("Xác nhận");

        jLabel7.setText("Nhập lại mật khẩu mới");

        jMenu6.setText("Điều khiển");

        jMenuItem6.setText("Thoát");
        jMenu6.add(jMenuItem6);

        jMenuBar3.add(jMenu6);

        jMenu7.setText("Trợ giúp");
        jMenuBar3.add(jMenu7);

        jDialog3.setJMenuBar(jMenuBar3);

        javax.swing.GroupLayout jDialog3Layout = new javax.swing.GroupLayout(jDialog3.getContentPane());
        jDialog3.getContentPane().setLayout(jDialog3Layout);
        jDialog3Layout.setHorizontalGroup(
            jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog3Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                    .addComponent(passwordTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(accountTextField1, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(43, 43, 43))
            .addGroup(jDialog3Layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(loginButton1)
                .addContainerGap(91, Short.MAX_VALUE))
        );
        jDialog3Layout.setVerticalGroup(
            jDialog3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog3Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(accountTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(loginButton1)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        clientlistScrollPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clientlistScrollPaneMouseClicked(evt);
            }
        });

        lstClients.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstClientsMouseClicked(evt);
            }
        });
        clientlistScrollPane.setViewportView(lstClients);

        sendButton.setText("Gửi");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        sendButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sendButtonKeyPressed(evt);
            }
        });

        okButton.setFont(new java.awt.Font("Tahoma", 1, 8));
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jToolBar1.setRollover(true);

        jLabel1.setText("Trạng thái người dùng");

        fileMenu.setText("File");

        connectMenuItem.setText("Kết nối");
        connectMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(connectMenuItem);

        configMenuItem.setText("Định dạng");
        fileMenu.add(configMenuItem);

        changepasswordMenuItem.setText("Thay đổi mật khẩu");
        changepasswordMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changepasswordMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(changepasswordMenuItem);

        logoutMenuItem.setText("Đăng xuất");
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(logoutMenuItem);

        exitMenuItem.setText("Thoát");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        jMenuBar1.add(fileMenu);

        tabMenu.setText("Tabs");
        jMenuBar1.add(tabMenu);

        helpMenu.setText("Help");
        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(messagetypeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tbChatWindows, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(statusTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(okButton))
                    .addComponent(clientlistScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statusTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clientlistScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(272, 272, 272)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(messagetypeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(26, 26, 26))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tbChatWindows, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(119, Short.MAX_VALUE))
        );

        tbChatWindows.getAccessibleContext().setAccessibleName("Phòng");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void connectMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectMenuItemActionPerformed
        // TODO add your handling code here:
        jDialog1.setVisible(true);       
        jDialog1.setBounds(641, 0, 253, 445);


        
    }//GEN-LAST:event_connectMenuItemActionPerformed

    private void accountTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accountTextFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_accountTextFieldActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void accountTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accountTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_accountTextField1ActionPerformed

    private void changepasswordMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changepasswordMenuItemActionPerformed
        // TODO add your handling code here:
        jDialog3.setVisible(true);
        jDialog3.setBounds(640, 0, 253, 445);
    }//GEN-LAST:event_changepasswordMenuItemActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        // TODO add your handling code here:
        sendMessage();
        messagetypeTextField.requestFocus();
    }//GEN-LAST:event_sendButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
         try {
                sendStatus();
            } catch (IOException ex) {
                Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
            }
			statusTextField.requestFocus();
    }//GEN-LAST:event_okButtonActionPerformed

    private void clientlistScrollPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clientlistScrollPaneMouseClicked
        // TODO add your handling code here:
        
    }//GEN-LAST:event_clientlistScrollPaneMouseClicked

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        
        client.Name = accountTextField.getText();
        client.Pass = passwordTextField.getText();
        jDialog1.dispose();
        if( client.connectToServer() ){

				jDialog1.setTitle( "Troy Chat Client <--> " + client.Name);

                                try{
				if( lstClients.getModel() != client.clientList )
				{
					lstClients.setModel( client.clientList );
				}
                                }catch(NullPointerException e)
                                                    {

                                }
				connectMenuItem.setEnabled( false );
                                logoutMenuItem.setEnabled(true);
				messagetypeTextField.setEnabled( true );
                                tbChatWindows.setEnabled(true);
                                statusTextField.setEnabled( true );
				sendButton.setEnabled( true );
                                okButton.setEnabled( true );
				lstClients.setEnabled( true );
                                changepasswordMenuItem.setEnabled(true);
                                exitMenuItem.setEnabled(true);
                                //encryptButton.setEnabled(false);
				((JTextArea)messageWindows.elementAt(0)).setText("");
				messagetypeTextField.requestFocus();
                                statusTextField.requestFocus();                                
           

                         }

                            

                            try {
                                client.sendRequestOffMsg(client.Name);
                            } catch (IOException ex) {

                            }
    }//GEN-LAST:event_loginButtonActionPerformed

    private void lstClientsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstClientsMouseClicked
        // TODO add your handling code here:
         if( !lstClients.isSelectionEmpty() )
     		{
	     		if ( evt.getClickCount() == 2 && evt.getButton() == MouseEvent.BUTTON1 )
    	 		{
                            String str[] = client.clientList.getElementAt(lstClients.getSelectedIndex()).toString().split(" ");
                            if(str[1].equalsIgnoreCase("test"))
                            {
                                JOptionPane.showMessageDialog( client.window,"Đây là tài khoản test, chỉ dùng để đăng ký","Lỗi chat",JOptionPane.ERROR_MESSAGE );
                            }
                            else if(str[0].equalsIgnoreCase("<<off>>"))
                            {
                                client.offMsg = JOptionPane.showInputDialog( client.window,"Người dùng đã thoát, hãy gửi tin offline");
                                  try {
                                        client.sendOfflineMessage(str[1]+":"+client.offMsg+":"+client.Name);
                                } catch (IOException ex) {
                                      Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            else if(str[1].equalsIgnoreCase(client.Name))
                            {
                                JOptionPane.showMessageDialog( client.window,"Bạn không thể chát với chính mình","Lỗi chat",JOptionPane.ERROR_MESSAGE );
                            }
                            else if(!str[0].equalsIgnoreCase("<<off>>")&&!str[0].equalsIgnoreCase("<<on>>"))
                            {
                                JOptionPane.showMessageDialog( client.window,"Đây chỉ là thông báo, không phải người dùng","Lỗi chat",JOptionPane.ERROR_MESSAGE );
                            }
                            else
                            {
					openNewTab( lstClients.getSelectedIndex() );
					client.sendChatRequest( lstClients.getSelectedIndex() );
                            }
      			}
      		}
    }//GEN-LAST:event_lstClientsMouseClicked

                private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
                    // TODO add your handling code here:
                    try {
                            client.sendLogout(true);
                            System.exit(0);
                    } catch (IOException ex) {
                            Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }//GEN-LAST:event_exitMenuItemActionPerformed

                private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
                    // TODO add your handling code here:
                    if(client.testconnect)
                    {
                        try {
                                //JOptionPane.showMessageDialog( client.window,"Bạn chỉ có thể thoát khi đã kết nối"+"\nvì chương trình client phải gửi thông báo cho server","Người dùng thoát",JOptionPane.INFORMATION_MESSAGE );
                                client.sendLogout(true);
                                System.exit(0);
                         } catch (IOException ex) {
                                Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
                         }
                    }
                    else{System.exit(0);}
                }//GEN-LAST:event_formWindowClosing

                private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed
                    // TODO add your handling code here:
                    try {
                                client.sendLogout(true);

                                connectMenuItem.setEnabled( true );
                                logoutMenuItem.setEnabled(false);
				messagetypeTextField.setEnabled( false );
                                tbChatWindows.setEnabled(false);
                                statusTextField.setEnabled( false );
				sendButton.setEnabled( false );
                                okButton.setEnabled( false );
				lstClients.setEnabled( false );
                                changepasswordMenuItem.setEnabled( false );
                                exitMenuItem.setEnabled(true);
				((JTextArea)messageWindows.elementAt(0)).setText("");

                         } catch (IOException ex) {
                                Logger.getLogger(HackerInterfaceDesign.class.getName()).log(Level.SEVERE, null, ex);
                         }
                }//GEN-LAST:event_logoutMenuItemActionPerformed

                private void sendButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sendButtonKeyPressed
                    // TODO add your handling code here:
                }//GEN-LAST:event_sendButtonKeyPressed

    /**
    * @param args the command line arguments
    */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField accountTextField;
    private javax.swing.JTextField accountTextField1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JMenuItem changepasswordMenuItem;
    private javax.swing.JScrollPane clientlistScrollPane;
    private javax.swing.JMenuItem configMenuItem;
    private javax.swing.JMenuItem connectMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuBar jMenuBar3;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton loginButton;
    private javax.swing.JButton loginButton1;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JList lstClients;
    private javax.swing.JTextField messagetypeTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JTextField passwordTextField1;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextField statusTextField;
    private javax.swing.JMenu tabMenu;
    private javax.swing.JTabbedPane tbChatWindows;
    // End of variables declaration//GEN-END:variables

}
